const path = require("path");

const resolve = function(dir) {
    return path.join(__dirname, dir);
};

module.exports = {
    outputDir: "exam_student",
    publicPath: "./",
    productionSourceMap: true,
    devServer: {
        open: true,
        overlay: {
            warnings: false,
            errors: true,
        },
        // before: require("./mock/mock-server.js"),
        // 跨域处理
        proxy: {
            "/api": {
                target: "http://47.112.139.110:1080", // 目标接口域名
                changeOrigin: true, // 是否跨域
                pathRewrite: {
                    "^/api": "/api", // 重写接口
                },
            },
        },
    },
    lintOnSave: false,
    chainWebpack: config => {
        // 设置路径别名。
        config.resolve.alias
            // .set("vue$", "vue/dist/vue.esm.js")
            .set("src", resolve("src"))
            .set("apis", resolve("src/apis"))
            .set("assets", resolve("src/assets"))
            .set("common", resolve("src/common"))
            .set("filters", resolve("src/common/filters"))
            .set("layouts", resolve("src/common/layouts"))
            .set("mixins", resolve("src/common/mixins"))
            .set("utils", resolve("src/common/utils"))
            .set("components", resolve("src/components"))
            .set("router", resolve("src/router"))
            .set("store", resolve("src/store"))
            .set("views", resolve("src/views"))
            .set("styles", resolve("src/styles"));

        // it can improve the speed of the first screen, it is recommended to turn on preload
        config.plugin("preload").tap(() => [
            {
                rel: "preload",
                // to ignore runtime.js
                // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
                fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
                include: "initial",
            },
        ]);

        // when there are many pages, it will cause too many meaningless requests
        config.plugins.delete("prefetch");

        config.when(process.env.NODE_ENV !== "development", config => {
            config
                .plugin("ScriptExtHtmlWebpackPlugin")
                .after("html")
                .use("script-ext-html-webpack-plugin", [
                    {
                        // `runtime` must same as runtimeChunk name. default is `runtime`
                        inline: /runtime\..*\.js$/,
                    },
                ])
                .end();
            config.optimization.splitChunks({
                chunks: "all",
                cacheGroups: {
                    libs: {
                        name: "chunk-libs",
                        test: /[\\/]node_modules[\\/]/,
                        priority: 10,
                        chunks: "initial", // only package third parties that are initially dependent
                    },
                    commons: {
                        name: "chunk-commons",
                        test: resolve("src/components"), // can customize your rules
                        minChunks: 3, //  minimum common number
                        priority: 5,
                        reuseExistingChunk: true,
                    },
                },
            });
            // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
            config.optimization.runtimeChunk("single");
        });
    },

    configureWebpack: {
        performance: {
            hints: "warning",
            //入口起点的最大体积 整数类型（以字节为单位）
            maxEntrypointSize: 50000000,
            //生成文件的最大体积 整数类型（以字节为单位 300k）
            maxAssetSize: 30000000,
            //只给出 js 文件的性能提示
            assetFilter: function(assetFilename) {
                return assetFilename.endsWith(".js");
            },
        },
    },
    // css: {
    //     requireModuleExtension: false,
    //     loaderOptions: {
    //         // 配置scss全局变量。
    //         scss: {
    //             prependData: `@import "~styles/variables.scss";`,
    //         },
    //     },
    // },
};
